<?php

class tx_chraetseltreff_convert2
{
    public $confArray = array ( "uploadPath" => "uploads/tx_chraetseltreff",
                                "datenanlieferung" => "fileadmin/Datenanlieferung",
                                "sysfolder" => "1332"
                               );
    
    public $taktung = array (   "0" => "+2 day",
                                "1" => "+5 day",
                                "2" => "+1 Month"
                              );
	
    public $kuerzel = array ( "0" => "Mon",
                              "1" => "Tue",
                              "2" => "Wed",
                              "3" => "Thu",
                              "4" => "Fri",
                              "5" => "Sat",
                              "6" => "Mon"
                            );
 
        // return timestamp of last Monday...Friday
    function last_dayofweek($day, $taktung, $week=0)
    {
        $kuerzel = $this->kuerzel[$day];
        $takt = $this->taktung[$taktung];
        $einsendeschluss = strtotime("last {$kuerzel} {$takt} {$week}");
        if ( $einsendeschluss >= time() )
        {
                $bool = true;	
        } else
        {
                $bool = false;
        }
        return $bool;
    }

    function getFirstDayMonth ($day, $y, $m, $taktung)
    {
        $takt = $this->taktung[$taktung];
        $kuerzel = $this->kuerzel[$day];
        
        if ((int)$m > 12)
        {
                $m = (int)1;
                $y = (int)$y+1;	
        }
        
        $einsendeschluss = strtotime($kuerzel." $y-$n $takt") ;
        
        if ( $einsendeschluss >= time() )
        {
                $bool = true;	
        } else
        {
                $bool = false;
        }
        return $bool;
    }
        
    public function convertMonth($arr, $number)
    {	
        if (!file_exists($this->confArray["uploadPath"]."/".$arr["filename"].sprintf("%02d", $number).".jpg"))
        {
            if (file_exists($this->confArray["datenanlieferung"]."/".strtoupper($arr["filename"]).sprintf("%02d", $number).$arr["suffix"]))
            {
                exec('convert '.$this->confArray["datenanlieferung"]."/".strtoupper($arr["filename"]).sprintf("%02d", $number).$arr["suffix"].
                 ' -colorspace RGB -scale 172x235 '.$this->confArray["uploadPath"]."/".$arr["filename"].sprintf("%02d", $number).".jpg");
                    
            } else
            {
                $pdf = strtoupper($arr["filename"]).sprintf("%02d", $number).$arr["suffix"];
                
                $mail = t3lib_div::makeInstance('t3lib_mail_message');
                $mail->setFrom(array($arr["emailreplyemail"] => $arr["emailreplyname"]));
                $mail->setTo(array($arr["emailtoemail"] => $arr["emailtoname"]));
                $mail->setSubject(str_replace("###MAGAZIN###",$pdf,$arr["emailsubject"]));
                $mail->setBody(str_replace("###MAGAZIN###",$pdf,$arr["emailbody"]));
                $mail->addPart(str_replace("###MAGAZIN###",$pdf,$arr["emailbody"]));
                $mail->send();                    
            }
            return false;
        } else
        {
            return true;	
        }
    }
    
    public function convertWeek($arr, $number)
    {
        if (!file_exists($this->confArray["uploadPath"]."/".$arr["filename"].$number.".jpg") )
        {	
            if (file_exists($this->confArray["datenanlieferung"]."/".strtoupper($arr["filename"]).$number.$arr["suffix"]))
            {
                    exec('convert '.$this->confArray["datenanlieferung"]."/".strtoupper($arr["filename"]).$number.$arr["suffix"].
                     ' -colorspace RGB -scale 172x235 '.$this->confArray["uploadPath"]."/".$arr["filename"].$number.".jpg");
            } else
            {
                    $pdf = strtoupper($arr["filename"]).$number.$arr["suffix"];
                    
                    $mail = t3lib_div::makeInstance('t3lib_mail_message');
                    $mail->setFrom(array($arr["emailreplyemail"] => $arr["emailreplyname"]));
                    $mail->setTo(array($arr["emailtoemail"] => $arr["emailtoname"]));
                    $mail->setSubject(str_replace("###MAGAZIN###",$pdf,$arr["emailsubject"]));
                    $mail->setBody(str_replace("###MAGAZIN###",$pdf,$arr["emailbody"]));
                    $mail->addPart(str_replace("###MAGAZIN###",$pdf,$arr["emailbody"]));
                    $mail->send();
            }
            return false;
        } else
        {
             return true;	
        }
    }

    public function main ($obj)
    {   
        $res = $GLOBALS [ 'TYPO3_DB' ]->exec_SELECTquery (
            'P.title PT, P.uid UID, evt_tag, evt_woche, filename, taktung, suffix, replyname, replyemail, P.email, R.*',
            'tx_chraetseltreff_print P LEFT JOIN tx_chraetseltreff_redaktion R ON P.email=R.uid',
            'P.pid='.$this->confArray["sysfolder"]. ' AND P.deleted=0 AND P.hidden=0',
            '',
            'sorting ASC'
        );				
         
        while ( $row = $GLOBALS [ 'TYPO3_DB' ]->sql_fetch_assoc ( $res ) )
        {
            if ( $row["evt_woche"] === "0" &&
                            $this->last_dayofweek($row["evt_tag"],
                            $row["taktung"],
                            "+1 week"
                            ) == true )
            {
                $weeknumber = (int)date("W")+1;                
                $this->convertWeek($row, $weeknumber);
                    
            } elseif ( $row [ "evt_woche"] === "1" )
            {
                $monthnumber = (int)date("m");
                
                if (  $this->getFirstDayMonth($row["evt_tag"],
                                            date("Y"), $monthnumber,
                                            $row["taktung"]
                                            ) == false )
                {
                    ++$monthnumber;
                    $this->convertMonth($row, $monthnumber);
                }
            }
        }
    }
}

class tx_chraetseltreff_convert extends tx_scheduler_Task
{
    public function execute()
    {
        $convert = t3lib_div::makeInstance("tx_chraetseltreff_convert2");
        $convert->main($this);
        return true;
    }
}
?>